package com.yodawnla.littleWar.card;

import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import com.yodawnla.littleWar.scene.BattleScene;
import com.yodawnla.littleWar.system.Tex;
import com.yodawnla.littleWar.system.Values;

public class CardMgr
{
	BattleScene mScene;
	static CardMgr instance = null;
	
	public static CardMgr getInstance()
	{
		if (instance == null)
			instance = new CardMgr();
		return instance;
	}
	
	public CardMgr()
	{
	}
	
	public void setScene(BattleScene scene)
	{
		mScene = scene;
	}
	
	public BattleScene getScene()
	{
		return mScene;
	}
		
	public HeroCard createHeroCard(String data)
	{
		HeroCard card = null;
		String[] cardInfo = data.split("!");
		int  job = Integer.valueOf(cardInfo[0]);
		int id = Integer.valueOf(cardInfo[1]);
		int star = Integer.valueOf(cardInfo[2]);
		int lv = Integer.valueOf(cardInfo[3]);
		int maxHp = Integer.valueOf(cardInfo[4]);
		int hp = Integer.valueOf(cardInfo[5]);
		int atk = Integer.valueOf(cardInfo[6]);
		int cost = 0;
		int exp = 0;
		if(cardInfo.length == 8)
		{
			cost = Integer.valueOf(cardInfo[7]);
			exp = Integer.valueOf(cardInfo[8]);
		}
		
		card = createHeroCard(job, id, star, lv, maxHp, hp, atk, cost, exp);
		
		return card;
	}
	
	public HandCard createHandCard(String data)
	{
		HandCard card = null;
		String[] cardInfo = data.split("!");
		String dataType = cardInfo[0];
		
		if(dataType.equals(Values.CardType.Hand))
		{
			int type = Integer.valueOf(cardInfo[1]);
			int damage = Integer.valueOf(cardInfo[2]);
			card = createHandCardCard(type, damage);
		}
		
		return card;
	}
	
	public HeroCard createHeroCard(int job, int id, int star, int lv, int maxHp, int hp, int atk, int cost, int exp)
	{
		HeroCard card = null;
		TiledTextureRegion tex = null;
		switch(job)
		{
		case Values.Job.Warrior:
			switch(id)
			{
			case 0:
				tex = mScene.getTiledTexture(Tex.Tiled.w0);
				break;
			case 1:
				tex = mScene.getTiledTexture(Tex.Tiled.w1);
				break;
			case 2:
				tex = mScene.getTiledTexture(Tex.Tiled.w2);
				break;
			}
			break;
		case Values.Job.Archer:
			switch(id)
			{
			case 0:
				tex = mScene.getTiledTexture(Tex.Tiled.a0);
				break;
			case 1:
				tex = mScene.getTiledTexture(Tex.Tiled.a1);
				break;
			case 2:
				tex = mScene.getTiledTexture(Tex.Tiled.a2);
				break;
			}
			break;
		case Values.Job.Sorcerer:
			switch(id)
			{
			case 0:
				tex = mScene.getTiledTexture(Tex.Tiled.s0);
				break;
			case 1:
				tex = mScene.getTiledTexture(Tex.Tiled.s1);
				break;
			case 2:
				tex = mScene.getTiledTexture(Tex.Tiled.s2);
				break;
			}
			break;
		default:
			tex = mScene.getTiledTexture(Tex.Tiled.w0);
			break;
		}
		card = new HeroCard(job, id, -1000, -1000, tex.deepCopy());
		card.setAbility(star, lv, maxHp, hp, atk, cost, exp);
		return card;
	}
	
	public HandCard createHandCardCard(int type, int damage)
	{		
		HandCard card = new HandCard(-1000, -1000, mScene.getTiledTexture(Tex.Tiled.handCard).deepCopy());
		card.setType(type);
		card.setDamage(damage);
		//card.setAbility(type, damage);
		
		return card;
	}
}